/**
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * This file is a part of the CANN Open Software.
 * Licensed under CANN Open Software License Agreement Version 1.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 */

/*!
 * \file fused_add_topk_div_proto.cpp
 * \brief
 */

#include <graph/utils/type_utils.h>
#include <register/op_impl_registry.h>
#include "log/ops_log.h"

using namespace ge;

namespace ops {
constexpr size_t TWO_DIM = 2;
constexpr size_t INPUT_X_IDX = 0;
constexpr size_t INPUT_ADD_NUM_IDX = 1;
constexpr size_t INPUT_MAPPING_NUM_IDX = 2;
constexpr size_t INPUT_MAPPING_TABLE_IDX = 3;
constexpr size_t OUTPUT_Y_IDX = 0;
constexpr size_t OUTPUT_INDICES_IDX = 1;
constexpr size_t ATTR_GROUP_NUM_IDX = 0;
constexpr size_t ATTR_GROUP_TOPK_IDX = 1;
constexpr size_t ATTR_N_IDX = 2;
constexpr size_t ATTR_K_IDX = 3;
constexpr size_t ATTR_ACTIVATE_TYPE_IDX = 4;
constexpr size_t ATTR_IS_NORM_IDX = 5;
constexpr size_t ATTR_ENABLE_EXPERT_MAPPING_IDX = 6;
constexpr size_t ATTR_SCALE_IDX = 7;

ge::graphStatus CheckMappingShape(gert::InferShapeContext *context, const int64_t expert_num)
{
    auto mapping_num_shape = context->GetInputShape(INPUT_MAPPING_NUM_IDX);
    OPS_LOG_E_IF_NULL(context, mapping_num_shape, return ge::GRAPH_FAILED);
    auto mapping_table_shape = context->GetInputShape(INPUT_MAPPING_TABLE_IDX);
    OPS_LOG_E_IF_NULL(context, mapping_table_shape, return ge::GRAPH_FAILED);
    if (mapping_num_shape->GetDimNum() != 1) {
        OPS_LOG_E(context, "the dimNum of mapping_num should be 1.");
        return ge::GRAPH_FAILED;
    }
    if (expert_num != mapping_num_shape->GetDim(0)) {
        OPS_LOG_E(context, "mapping_num.shape[0] should be equal to x.shape[1].");
        return ge::GRAPH_FAILED;
    }
    if (mapping_table_shape->GetDimNum() != TWO_DIM) {
        OPS_LOG_E(context, "the dimNum of mapping_table should be 2.");
        return ge::GRAPH_FAILED;
    }
    if (expert_num != mapping_table_shape->GetDim(0)) {
        OPS_LOG_E(context, "mapping_table.shape[0] should be equal to x.shape[1].");
        return ge::GRAPH_FAILED;
    }

    return ge::GRAPH_SUCCESS;
}

ge::graphStatus InferShapeForFusedAddTopkDiv(gert::InferShapeContext *context)
{
    OPS_LOG_I(context, "Enter InferShapeForFusedAddTopkDiv runtime infershape impl.");
    auto x_shape = context->GetInputShape(INPUT_X_IDX);
    OPS_LOG_E_IF_NULL(context, x_shape, return ge::GRAPH_FAILED);
    auto add_num_shape = context->GetInputShape(INPUT_ADD_NUM_IDX);
    OPS_LOG_E_IF_NULL(context, add_num_shape, return ge::GRAPH_FAILED);
    if (x_shape->GetDimNum() != TWO_DIM) {
        OPS_LOG_E(context, "the dimNum of x should be 2.");
        return ge::GRAPH_FAILED;
    }
    if (add_num_shape->GetDimNum() != 1) {
        OPS_LOG_E(context, "the dimNum of add_num should be 1.");
        return ge::GRAPH_FAILED;
    }
    auto expert_num = x_shape->GetDim(1);
    if (expert_num != add_num_shape->GetDim(0)) {
        OPS_LOG_E(context, "add_num.shape[0] should be equal to x.shape[1].");
        return ge::GRAPH_FAILED;
    }

    auto attrs = context->GetAttrs();
    OPS_LOG_E_IF_NULL(context, attrs, return ge::GRAPH_FAILED);

    auto enable_expert_mapping_ptr = attrs->GetAttrPointer<bool>(ATTR_ENABLE_EXPERT_MAPPING_IDX);
    OPS_LOG_E_IF_NULL(context, enable_expert_mapping_ptr, return ge::GRAPH_FAILED);
    auto enable_expert_mapping = *enable_expert_mapping_ptr;
    if (enable_expert_mapping) {
        if (CheckMappingShape(context, expert_num) == ge::GRAPH_FAILED) {
            return ge::GRAPH_FAILED;
        }
    }

    auto y_shape = context->GetOutputShape(OUTPUT_Y_IDX);
    OPS_LOG_E_IF_NULL(context, y_shape, return ge::GRAPH_FAILED);
    auto indices_shape = context->GetOutputShape(OUTPUT_INDICES_IDX);
    OPS_LOG_E_IF_NULL(context, indices_shape, return ge::GRAPH_FAILED);
    *y_shape = *x_shape;
    *indices_shape = *x_shape;
    auto k_ptr = attrs->GetAttrPointer<bool>(ATTR_K_IDX);
    OPS_LOG_E_IF_NULL(context, k_ptr, return ge::GRAPH_FAILED);
    auto k = *k_ptr;
    y_shape->SetDim(1, k);
    indices_shape->SetDim(1, k);

    return ge::GRAPH_SUCCESS;
}

ge::graphStatus InferDataTypeForFusedAddTopkDiv(gert::InferDataTypeContext *context)
{
    if (context == nullptr) {
        return ge::GRAPH_FAILED;
    }
    context->SetOutputDataType(0, ge::DT_FLOAT);
    context->SetOutputDataType(1, ge::DT_INT32);
    return ge::GRAPH_SUCCESS;
}

IMPL_OP_INFERSHAPE(FusedAddTopkDiv)
    .InferShape(InferShapeForFusedAddTopkDiv)
    .InferDataType(InferDataTypeForFusedAddTopkDiv);

} // namespace ops
